package cn.huihui.miniprogram.stuman.component;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestControllerAdvice
public class MiniProgramExceptionHandler {

    public MiniProgramExceptionHandler() {
    }

    /**
     * 如果返回的是内部类的话，这里是不会替换到返回的异常中的（可能是序列化失败）
     * @return
     */
    @ExceptionHandler(Exception.class)
    public MiniProgramErrorMsg handleException(HttpServletResponse response,HttpServletRequest request, Exception e){
        MiniProgramErrorMsg msg = new MiniProgramErrorMsg();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request2 = servletRequestAttributes.getRequest();
        HttpServletResponse response2 = servletRequestAttributes.getResponse();
        msg.setMessage(e.getMessage());
        msg.setCode(500);
        return msg;
    }




}
